import {
  getGame,
  addClick,
  addOpen
} from "../../api/getApi"


Page({

  data: {
    tabIndex:wx.getStorageSync('tabIndex') ? wx.getStorageSync('tabIndex') : 0,
    isUp: false,
    tab1Index: wx.getStorageSync('tab1Index') ? wx.getStorageSync('tab1Index') : 1,
    tab2Index: wx.getStorageSync('tab2Index') ?wx.getStorageSync('tab2Index') : 0,
    gameData: [],
    page:1,
    limit:35,
    slidList: [
      {
        "category2Name": "休闲益智",
        "category2Id": 1
      },
      {
        "category2Name": "角色扮演",
        "category2Id": 2
      },
      {
        "category2Name": "动作射击",
        "category2Id": 3
      },
      {
        "category2Name": "棋牌桌游",
        "category2Id": 5
      },
      {
        "category2Name": "竞技对战",
        "category2Id": 7
      },
      {
        "category2Name": "其他游戏",
        "category2Id": 0
      },
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({gameData:[],page:1})
    this.getData() 
  },

  tabClick(event) {

    if (event.target.dataset.data == "0") {
      this.setData({
        tabIndex: 0
      })
      wx.setStorageSync('tabIndex', 0)
    }
    if (event.target.dataset.data == "1") {
      this.setData({
        tabIndex: 1
      })
      wx.setStorageSync('tabIndex', 1)
    }
    this.setData({gameData:[],page:1})
    this.getData() 
  },
  getData() {
    getGame({
      page: this.data.page,
      limit: this.data.limit,
      hot: this.data.tab2Index == 0 && this.data.tabIndex == 1 ? 'true' : 'false',
      openHot: this.data.tab2Index == 1 && this.data.tabIndex == 1 ? 'true' : 'false',
      category2Id: this.data.tabIndex == 0 ? this.data.tab1Index : '',
    }).then(res => {
      const list = res.list;
      if (list && list.length > 0) {
        this.setData({
            gameData: [...this.data.gameData,...list]
        });
      }
    })
  },
  goToGame(e){
    let itemdata = e.currentTarget.dataset.data
    if (itemdata.appid){
      wx.navigateToMiniProgram({
        appId: itemdata.appid, 
        path: '?wxgamepro=CpsCBgAAoXkpQY9UXS9__OwV',
        envVersion: 'release',
        success:(res)=>{
          let myGame = wx.getStorageSync('myGame')
          if(!myGame){
            myGame = []
          }else{
            myGame = JSON.parse(myGame).slice(0, 300)
          }
          if(!myGame.find(v=>v.id===itemdata.id)){
            myGame=[itemdata,...myGame]
          }
          wx.setStorageSync('myGame', JSON.stringify(myGame))
          this.setData({myGame:myGame})
          addOpen({id:itemdata.id})
        }
      })
      addClick({id:itemdata.id})
    }else{
      wx.showToast({
        title: `尊敬的大人对对不不起起！此游戏出了点小问题~`,
        icon: 'none',
      });
    }
  },
  //第一个标签页tag标签
  changeTag1(e) {
    let index =e.currentTarget.dataset.data
    this.setData({
      tab1Index: index
    })
    wx.setStorageSync('tab1Index', index)
    this.setData({gameData:[],page:1})
    this.getData() 
  },
  //第二个标签页tag标签
  changeTag2(e) {
    let index = e.currentTarget.dataset.data
    this.setData({
      tab2Index: index
    })
    wx.setStorageSync('tab2Index', index)
    this.setData({gameData:[],page:1})
    this.getData() 
  },

  up(e) {
    this.setData({
      isUp: false
    })
    this.setData({gameData:[],page:1})
    this.getData() 
    this.setData({
      isUp: true
    })
  },
  down(e) {
    this.setData({page:this.data.page+1})
    this.getData() 
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      tabIndex:wx.getStorageSync('tabIndex') ? wx.getStorageSync('tabIndex') : 0,
      tab1Index: wx.getStorageSync('tab1Index') ? wx.getStorageSync('tab1Index') : 1,
      tab2Index: wx.getStorageSync('tab2Index') ?wx.getStorageSync('tab2Index') : 0,
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})